package cn.xorange.commons.data.dao;

import cn.xorange.commons.data.build.DeleteSqlBuild;
import cn.xorange.commons.data.build.InsertSqlBuild;
import cn.xorange.commons.data.build.SelectSqlBuild;
import cn.xorange.commons.data.build.UpdateSqlBuild;
import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.exception.DataException;
import cn.xorange.commons.utils.lang.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : yangjian
 * @date : 2023/12/31
 */
public class CrudDao {

    public <T extends BaseEntity> String add(T t){
        InsertSqlBuild<T> insertSql = new InsertSqlBuild<>();
        insertSql.insert(t);
        return t.p.getSql();
    }

    public <T extends BaseEntity> String addBath(Map<String, Object> map) {
        List<T> tList = (List<T>) map.get("list");
        String sql = null;
        if (tList.isEmpty()) {
            throw DataException.dataValueNull("批量新增数据为空");
        } else {
            InsertSqlBuild<T> insertSql = new InsertSqlBuild<T>();
            Map<String, Object> batchMap = insertSql.insertBath(tList);
            map.put("list", batchMap.get("param"));
            sql = ObjectUtils.toString(batchMap.get("sql"));
        }
        return sql;
    }
    public <T extends BaseEntity> String addMapBath(Map<String, Object> map) {
        List<Map<String,Object>> mapList = (List<Map<String,Object>>)map.get("mapList");
        String sql = null;
        if (mapList.isEmpty()) {
            throw DataException.dataValueNull("批量新增数据为空");
        } else {
            InsertSqlBuild<T> insertSql = new InsertSqlBuild<>();
            String tableName = map.get("tableName").toString();
            Map<String, Object> batchMap = insertSql.insertBathMap(tableName,mapList);
            map.put("list",batchMap.get("param"));
            sql = ObjectUtils.toString(batchMap.get("sql"));
        }
        return sql;
    }

    public <T extends BaseEntity> String del(T t) {
        DeleteSqlBuild<T> deleteSql = new DeleteSqlBuild<>();
        deleteSql.delete(t);
        return t.p.getSql();
    }

    public <T extends BaseEntity> String delById(Map<String, Object> map) {
        Object id = map.get("id");
        if (Objects.isNull(id)){
            throw DataException.dataKeyValueNull();
        }
        Class<T> tClass = (Class<T>) map.get("arg1");
        DeleteSqlBuild<T> deleteSql = new DeleteSqlBuild<>();
        return deleteSql.deleteById(tClass);
    }

    public <T extends BaseEntity> String truncate(T t){
        DeleteSqlBuild<T> deleteSql = new DeleteSqlBuild<>();
        return deleteSql.truncate(t);
    }

    public <T extends BaseEntity> String update(T t) {
        UpdateSqlBuild<T> updateSql = new UpdateSqlBuild<>();
        updateSql.update(t);
        return t.p.getSql();
    }

    public <T extends BaseEntity> String selectById(Map<String,Object> map){
        Object id = map.get("id");
        if (Objects.isNull(id)){
            throw DataException.dataKeyValueNull();
        }
        Class<T> tClass = (Class<T>) map.get("arg1");
        SelectSqlBuild<T> selectSql = new SelectSqlBuild<>();
        return selectSql.selectById(tClass);
    }
    public <T extends BaseEntity> String selectOne(T t){
        SelectSqlBuild<T> selectSql = new SelectSqlBuild<>();
        t.setSelectLimit(1);
        selectSql.select(t);
        return t.p.getSql();
    }
    public <T extends BaseEntity> String selectList(T t){
        SelectSqlBuild<T> selectSql = new SelectSqlBuild<>();
        selectSql.select(t);
        return t.p.getSql();
    }

    public <T extends BaseEntity> String selectCount(T t){
        SelectSqlBuild<T> selectSql = new SelectSqlBuild<>();
        selectSql.count(t);
        return t.p.getSql();
    }
}
